package br.com.fumec.maze;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class State {
	int width = 0;
	int height = 0;
	
	Position position;
	Objective objective;
	Moves moves;

	public static State newInstance(Element element){
		State state = new State();
	        
		state.width = Integer.parseInt(element.getAttribute("width"));
		state.height = Integer.parseInt(element.getAttribute("height"));
		
		int x = Integer.parseInt(((Element)element.getElementsByTagName("position").item(0)).getAttribute("x"));
		int y = Integer.parseInt(((Element)element.getElementsByTagName("position").item(0)).getAttribute("y"));
		Position position = new Position(x, y);
		state.setPosition(position);
		
		x = Integer.parseInt(((Element)element.getElementsByTagName("objective").item(0)).getAttribute("x"));
		y = Integer.parseInt(((Element)element.getElementsByTagName("objective").item(0)).getAttribute("y"));
		Objective objective = new Objective(x, y);
		state.setObjective(objective);
		
		boolean left = false;
		boolean right = false;
		boolean up = false;
		boolean down = false;
		
		NodeList movesNode = ((Element)element.getElementsByTagName("moves").item(0)).getChildNodes(); 
		for(int i = 0; i < movesNode.getLength(); i++){
			if(((Element)movesNode.item(i)).getNodeName().equalsIgnoreCase("left")){
				left = true;	
			}
			
			if(((Element)movesNode.item(i)).getNodeName().equalsIgnoreCase("right")){
				right = true;	
			}
			
			if(((Element)movesNode.item(i)).getNodeName().equalsIgnoreCase("up")){
				up = true;	
			}
			
			if(((Element)movesNode.item(i)).getNodeName().equalsIgnoreCase("down")){
				down = true;	
			}			 
		}
		
		Moves moves = new Moves(left, right, up, down);
		state.setMoves(moves);
		
		return state;
	}
	
	public int getWidth() {
		return width;
	}
	public void setWidth(int width) {
		this.width = width;
	}
	public int getHeight() {
		return height;
	}
	public void setHeight(int height) {
		this.height = height;
	}
	public Position getPosition() {
		return position;
	}
	public void setPosition(Position position) {
		this.position = position;
	}
	public Objective getObjective() {
		return objective;
	}
	public void setObjective(Objective objective) {
		this.objective = objective;
	}
	public Moves getMoves() {
		return moves;
	}
	public void setMoves(Moves moves) {
		this.moves = moves;
	}
}